//
// Created by 张易生 on 2022/1/12.
//

#include "ZYSCallJava.h"


ZYSCallJava::ZYSCallJava(_JavaVM *javaVM, JNIEnv *env, jobject *obj) {

    this->javaVM = javaVM;
    this->jniEnv = env;
    this->jobj = *obj;
    this->jobj = env->NewGlobalRef(jobj);

    jclass  jlz = jniEnv->GetObjectClass(jobj);
    if(!jlz){
        if(LOG_DEBUG){
            LOGE("get jclass wrong");
        }
        return;
    }

    jmid_prepared = env->GetMethodID(jlz, "onCallPrepared", "(I)V");
    jmid_time = env->GetMethodID(jlz, "onCallTimeInfo", "(II)V");
    jmid_load = env->GetMethodID(jlz, "onCallLoad", "(Z)V");
    jmid_renderyuv = env->GetMethodID(jlz, "onCallRenderYUV", "(II[B[B[B)V");
}

void ZYSCallJava::onCallPrepared(int type,int total) {

    if(type == MAIN_THREAD){
        jniEnv->CallVoidMethod(jobj, jmid_prepared,total);
    }else if(type == CHILD_THREAD){
        JNIEnv *jniEnv;
        if(javaVM->AttachCurrentThread(&jniEnv, 0) != JNI_OK){
            if(LOG_DEBUG){
                LOGE("get child thread jnienv worng");
            }
            return;
        }
        jniEnv->CallVoidMethod(jobj, jmid_prepared,total);
        javaVM->DetachCurrentThread();
    }

}

void ZYSCallJava::onCallTimeInfo(int type, int current, int total) {
    if(type == MAIN_THREAD){
        jniEnv->CallVoidMethod(jobj, jmid_time,current,total);
    }else if(type == CHILD_THREAD){
        JNIEnv *jniEnv;
        if(javaVM->AttachCurrentThread(&jniEnv, 0) != JNI_OK){
            if(LOG_DEBUG){
                LOGE("get child thread jnienv worng");
            }
            return;
        }
        jniEnv->CallVoidMethod(jobj, jmid_time,current,total);
        javaVM->DetachCurrentThread();
    }
}

void ZYSCallJava::onCallLoad(int type, bool load) {
    if(type == MAIN_THREAD){
        jniEnv->CallVoidMethod(jobj, jmid_load,load);
    }else if(type == CHILD_THREAD){
        JNIEnv *jniEnv;
        if(javaVM->AttachCurrentThread(&jniEnv, 0) != JNI_OK){
            if(LOG_DEBUG){
                LOGE("get child thread jnienv worng");
            }
            return;
        }
        jniEnv->CallVoidMethod(jobj, jmid_load,load);
        javaVM->DetachCurrentThread();
    }
}

void ZYSCallJava::onCallRenderYUV(int width, int height, uint8_t *fy, uint8_t *fu,
                                  uint8_t *fv) {
    JNIEnv *jniEnv;
    if(javaVM->AttachCurrentThread(&jniEnv, 0) != JNI_OK){
        if(LOG_DEBUG){
            LOGE("call onCallComplete worng");
        }
        return;
    }

    jbyteArray y = jniEnv->NewByteArray(width * height);
    jniEnv->SetByteArrayRegion(y, 0, width * height, reinterpret_cast<const jbyte *>(fy));

    jbyteArray u = jniEnv->NewByteArray(width * height / 4);
    jniEnv->SetByteArrayRegion(u, 0, width * height / 4, reinterpret_cast<const jbyte *>(fu));

    jbyteArray v = jniEnv->NewByteArray(width * height / 4);
    jniEnv->SetByteArrayRegion(v, 0, width * height / 4, reinterpret_cast<const jbyte *>(fv));

    jniEnv->CallVoidMethod(jobj, jmid_renderyuv, width, height, y, u, v);
    jniEnv->DeleteLocalRef(y);
    jniEnv->DeleteLocalRef(u);
    jniEnv->DeleteLocalRef(v);
    javaVM->DetachCurrentThread();
}

ZYSCallJava::~ZYSCallJava() {

}




